package com.cdm.beans.cdm;

import java.util.List;

import net.sf.frails.JSFHelper;
import net.sf.frails.aop.annotations.InjectBean;
import net.sf.frails.bean.annotations.DefMbean;
import net.sf.frails.bean.annotations.InVar;
import net.sf.frails.bean.annotations.PreRender;
import net.sf.frails.bean.annotations.Prop;

import com.cdm.persistence.ContentType;
import com.cdm.persistence.EntityManagerHelper;
import com.cdm.persistence.User;
import com.cdm.util.Constants;
import com.vivo.util.DTool;

@DefMbean
public class ContentTypeBean {

	@Prop
	User loginUser;

	@Prop
	List<ContentType> contentTypes;

	@InVar
	@Prop
	ContentType contentType;

	@SuppressWarnings("unchecked")
	@PreRender(failOutcome = "/content/contentType")
	public boolean init() {
		Object object = JSFHelper.getFromApplication(Constants.LOGIN_USER_KEY);
		if (object instanceof User && object != null) {
			loginUser = (User) object;
		}
		contentTypes = EntityManagerHelper.getEntityManager().createQuery("from ContentType").getResultList();
		return true;
	}



	@InjectBean
	public void delete() {
		try {
			EntityManagerHelper.beginTransaction();
			EntityManagerHelper.getEntityManager().remove(EntityManagerHelper.getEntityManager().merge(contentType));
			EntityManagerHelper.commit();
		} catch (RuntimeException e) {
			JSFHelper.addMessage(e.getLocalizedMessage());
			try {
				EntityManagerHelper.rollback();
			} catch (RuntimeException ex) {
				ex.printStackTrace();
			}
		}
	}
}
